
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Tathata.Common.BLL.Entities;
using Tathata.Common.BLL.Managers;
using Tathata.Common.Utils;

namespace Tathata.Web.Administration
{
    public partial class Administration_DiscountAdd : BaseNopAdministrationPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                SetDefaultValues();
            }
        }

        private void SetDefaultValues()
        {
            txtStartDate.Text = DateTime.Now.AddDays(-1).ToString(cStartDateButtonExtender.Format);
            txtEndDate.Text = DateTime.Now.AddMonths(6).ToString(cEndDateButtonExtender.Format);
        }

        protected void AddButton_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    DateTime discountStartDate = DateTime.MinValue;
                    DateTime discountEndDate = DateTime.MinValue;
                    if (!DateTime.TryParse(txtStartDate.Text, out discountStartDate))
                        throw new Exception("Start date is not set");
                    if (!DateTime.TryParse(txtEndDate.Text, out discountEndDate))
                        throw new Exception("End date is not set");

                    Discount discount = DiscountManager.InsertDiscount(txtName.Text, cbUsePercentage.Checked, txtDiscountPercentage.Value,
                           txtDiscountAmount.Value, discountStartDate, discountEndDate, false);

                    Response.Redirect("DiscountDetails.aspx?DiscountID=" + discount.DiscountID.ToString());
                }
                catch (Exception exc)
                {
                    ProcessException(exc);
                }
            }
        }
    }
}
